export class SheetUtil {

  /**
   * @description 获取Luckysheet对象
   * @static
   * @return {*}  {*}
   * @memberof SheetUtil
   */
  public static getLuckySheet(): any {
    return (window as any).luckysheet;
  }

  /**
   * @description 获取Luckysheet当前工作表
   * @return {*} 
   * @memberof SheetUtil
   */
  public static getSheet(): any {
    return (window as any).luckysheet.getSheet();
  }

  /**
   * @description 创建luckysheet
   * @static
   * @param {*} options luckysheet配置
   * @memberof SheetUtil
   */
  public static create(options: any) {
    const luckySheet = this.getLuckySheet();
    if (luckySheet && options) {
      luckySheet.create(options);
    }
  }

  /**
   * @description 销毁
   * @static
   * @memberof SheetUtil
   */
  public static destroy() {
    const luckySheet = this.getLuckySheet();
    if (luckySheet) {
      luckySheet.destroy();
    }
  }

  /**
   * @description 设置工作表配置
   * @param {*} config
   * @memberof SheetUtil
   */
  public static setConfig(config: any, isMerge: boolean = false) {
    const luckySheet = this.getLuckySheet();
    if (isMerge) {
      const rootConfig = Object.assign(luckySheet.getConfig(), config);
      luckySheet.setConfig(rootConfig);
    } else {
      luckySheet.setConfig(config);
    }
  }

  /**
   * @description 获取当前工作表配置
   * @static
   * @return {*} 
   * @memberof SheetUtil
   */
  public static getConfig() {
    const luckySheet = this.getLuckySheet();
    if (luckySheet) {
      return luckySheet.getConfig();
    }
  }

  /**
   * @description 设置单元格值
   * @static
   * @param {number} row 行
   * @param {number} col 列
   * @param {*} value 单元格值
   * @memberof SheetUtil
   */
  public static setCellValue(row: number, col: number, value: any) {
    const luckySheet = this.getLuckySheet();
    if (luckySheet) {
      luckySheet.setCellValue(row, col, value);
    }
  }

  /**
   * @description 指定工作表选区是否高亮
   * @static
   * @param {*} range 选区
   * @param {*} [opt] 配置 { show: boolean, order: number, success: Function }
   * @memberof SheetUtil
   */
  public static setRangeShow(range: any, opt?: any) {
    const luckySheet = this.getLuckySheet();
    if (luckySheet) {
      luckySheet.setRangeShow(range, opt);
    }
  }

  /**
   * @description 获取列宽度
   * @static
   * @param {any[]} columnInfo 列下标数组
   * @param {*} [opt]
   * @memberof SheetUtil
   */
  public static getColumnWidth(columnInfo: any[], opt?: any) {
    const luckySheet = this.getLuckySheet();
    if (luckySheet && columnInfo.length > 0) {
      return luckySheet.getColumnWidth(columnInfo, opt);
    }
  }

  /**
   * @description 获取行高度
   * @static
   * @param {any[]} rowInfo 行下标数组
   * @param {*} [opt]
   * @memberof SheetUtil
   */
  public static getRowHeight(rowInfo: any[], opt?: any) {
    const luckySheet = this.getLuckySheet();
    if (luckySheet && rowInfo.length > 0) {
      return luckySheet.getRowHeight(rowInfo, opt);
    }
  }

  /**
   * @description 获取默认行高
   * @static
   * @memberof SheetUtil
   */
  public static getDefaultRowHeight(): number {
    const luckySheet = this.getLuckySheet();
    if (luckySheet) {
      return luckySheet.getDefaultRowHeight() as number;
    }
    return 20;
  }

  /**
   * @description 清空单元格
   * @static
   * @param {number} row 行下标
   * @param {number} col 列下标
   * @memberof SheetUtil
   */
  public static clearCell(row: number, col: number) {
    const luckySheet = this.getLuckySheet();
    if (luckySheet) {
      luckySheet.clearCell(row, col);
    }
  }

  /**
   * @description 获取元素
   * @static
   * @param {string} tag 元素标识
   * @return {*} 
   * @memberof SheetUtil
   */
  public static getElement(tag: string) {
    return (window as any).$(`${tag}`);
  }
}